var app = getApp();
import ajaxhelper from '../../utils/ajaxhelper.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 1, //预设当前项的值
    dataList: [],//列表数据
    hasRefresh: false,
    isGetMore: false,
    hasMore: true,
    pageSize: 10,
    pageNo: 1,
    isShow: false,//是否显示没有列表的提示图
    subjectId: '',//类型id
    name: '',
    totalPrice: '',
    type: '',
    showView: false,
    arr: [],
    mediaType: '',
    isshow: true,
    item: '编辑',
    brr: [],
    i: '',
    index: '',
    currentId: '',
    ishide: true,
    image: '../../images/delete.png',
    idx: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    console.log(option)
    this.getListData(this.data.currentTab); //获取列表数据
  },

  //列表渲染数据
  getListData: function (id) {
    var type;
    if (id == '1') {
      type = 'AUDIO'
    } else if (id == '2') {
      type = 'VIDEO'
    }
    let param = {
      "mediaType": type,
      "pageSize": this.data.pageSize,
      "pageNo": this.data.pageNo,
    }
    var token = wx.getStorageSync('Token_date') && wx.getStorageSync('Token_date').Token;
    ajaxhelper.post(app.globalData.frontJSHost + "knowledge/myPaid/" + this.data.pageNo + "/" + this.data.pageSize +"?token=" + encodeURIComponent(token), param, this, this.setListData)
  },
  setListData(res) {
    console.log(res, 677)

    if (res.result) {

      var Data = res.data;
      var Result = Data.result || '';
      var length = Result.length;
      var pageNo = this.data.pageNo;
      var hasmore = Data.totalCount - pageNo * this.data.pageSize;
      this.setData({
        pageNo: ++pageNo,
        hasRefresh: false,
        isGetMore: false,
        hasMore: hasmore,
        brr: []
        // tip: '上拉加载更多数据'
      })

      if (Data && Result) {
        this.setData({
          dataList: this.data.dataList.concat(Result),
        })
      }
      console.log(this.data.dataList)
    }else{
      this.setData({
        hasRefresh: false,
        isGetMore: false,
      })
      wx.showToast({
        title: !!res.message.global ? res.message.global : "请求出错",
        icon: "loading"
      })
    }
    this.setData({
      isShow: true
    })
  }
  ,

  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;

    if (this.data.currentTab == id) { return false; }
    else {
      this.setData({
        currentTab: id,
        dataList: [],
        pageNo: 1,
        showView: false,
        item: '编辑',
        image: '../../images/delete.png'
      })
    }
    this.getListData(id);//获取列表数据
  },


  //跳转专辑详情
  toalbum: function (event) {
    var id = event.currentTarget.dataset.id;
    var mediaType = event.currentTarget.dataset.mediaType;
    console.log(id)
    console.log(mediaType)
    if (mediaType == 'AUDIO') {
      wx.navigateTo({
        url: '../audioAlbumDetail/audioAlbumDetail?id=' + id
      })
    } else {
      wx.navigateTo({
        url: '../videoAlbumDetail/videoAlbumDetail?id=' + id
      })
    }
  },

  //下拉刷新
  onPullDownRefresh() {
    this.setData({
      isShow: false
    })
    if (this.data.hasRefresh) return false;
    this.setData({
      dataList: [],
      hasRefresh: true,
      pageNo: 1
    })
    this.getListData(this.data.currentTab)
    setTimeout(function () {
      wx.stopPullDownRefresh()
    }, 1400);
  },
  //上拉加载
  onReachBottom() {
    if (this.data.isGetMore) return false;
    if (this.data.hasRefresh) return false;
    if (this.data.hasMore > 0) {
      this.setData({
        isGetMore: true,
        // tip: '正在加载中...'
      })
      this.getListData(this.data.currentTab);
    } else {
      this.setData({
        isGetMore: false,
        // tip: '没有更多数据了...'
      })
      return false
    }
  },
})